/**
 * 主页
 * Created by KY on 2017/7/26.
 */
define(['ldfw/core','text!tpl/main.tpl','comps/session','page/position/ListView','page/position/ContributionView',
        'page/product/ListView','page/dividend/ListView','page/user/IndexView','page/news/IndexView'],
    function(LDFW,tpl,session,PositionListView,ContributionView,ProductListView,
             DividendListView,UserIndexView,NewsIndexView){
    let MainView = LDFW.Container.extend({
        tpl:tpl,
        className:'panel',
        events:{
            'click .weui-tabbar__item':'changeTab',
        },
        initialize:function(){
        },
        setDefaultIndex:function(index){
            if(index===null||index===undefined){
                this.defaultIndex = 1
            }else{
                this.defaultIndex = index
            }
        },
        rendered:function(){
            const _this = this
            let tabbar = this.$el.find(".weui-tabbar__item"),
                tabcontent = this.$el.find(".weui-tab__bd-item"),
                tabview = this.tabview = [
                    new NewsIndexView({el:this.$el.find('#news')[0]}),
                    new ProductListView({el:this.$el.find('#product')[0]}),
                    new PositionListView({el:this.$el.find('#position')[0]}),
                    new DividendListView({el:this.$el.find('#dividend')[0]}),
                    new UserIndexView({el:this.$el.find('#me')[0]})
                ]
            this.off('shown')
            tabview.forEach(function(view,index){
                //转发shown事件
                view.stopListening(_this,'shown')
                view.listenTo(_this,'shown',function(){
                    if(view.$el.parent('.weui-tab__bd-item').css('display') === 'block'){
                        view.trigger('shown')
                    }
                })
                view.render()
            })
            tabbar.eq(this.defaultIndex).addClass('weui-bar__item_on')
            tabcontent.eq(this.defaultIndex).css('display','block')
            this.onTabChange.call(this,this.defaultIndex,tabcontent.eq(this.defaultIndex))
        },
        changeTab:function(event){
            const self = this
            event.preventDefault()
            var tabcontents = this.$el.find(".weui-tab__bd-item")
            if($(event.currentTarget).hasClass('weui-bar__item_on')){
                return
            }
            $(event.currentTarget).addClass('weui-bar__item_on')
                .siblings('.weui-bar__item_on')
                .removeClass('weui-bar__item_on')
            var target = $(event.currentTarget).attr('target')
            tabcontents.hide()
            tabcontents.each(function(index,tabcontent){
                if($(tabcontent).attr('id') === target){
                    $(tabcontent).show()
                    self.tabview[index].trigger('shown')
                    self.onTabChange.call(self,index,tabcontent)
                }
            })
        },
        onTabChange:function(index,tab){
            //用来缓存页面的tabindx
            this.trigger('change:tabindex',index)
        }
    })
    return new MainView()
})